﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using Jumpton.SFA.Server.BusinessRule;
using System.Data;
using System.Text;
/// <summary>
///OrderService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。 
// [System.Web.Script.Services.ScriptService]
public class OrderService : System.Web.Services.WebService
{

    public OrderService()
    {

        //如果使用设计的组件，请取消注释以下行 
        //InitializeComponent(); 
    }

    //[WebMethod]
    //[System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    //public string GetHistoryOrderList(int userId, long orderDate)
    //{

    //    StringBuilder sb = new StringBuilder();
    //    //返回消息内容
    //    String Message = "";
    //    sb.Append("{");
    //    try
    //    {
    //        DataTable dataApp = new OrderHeaderManager().GetHistoryOrderList(userId, orderDate);
    //        sb.Append(" \"HistoryOrderList\":[");
    //        if (dataApp != null && dataApp.Rows.Count > 0)
    //        {
    //            for (int i = 0; i < dataApp.Rows.Count; i++)
    //            {
    //                sb.Append("{");
    //                String orderNo = "";
    //                for (int j = 0; j < dataApp.Columns.Count; j++)
    //                {
    //                    sb.Append("\"" + dataApp.Columns[j].ColumnName.ToString() + "\":\"" + dataApp.Rows[i][j].ToString() + "\",");
    //                    if ("ORDER_NO" == dataApp.Columns[j].ColumnName)
    //                    {
    //                        orderNo = dataApp.Rows[i][j].ToString();
    //                    }
    //                }

    //                DataTable dataLine = new OrderLineManager().GetOrderLineList(orderNo);
    //                sb.Append(" \"OrderLineList\":[");
    //                if (dataLine != null && dataLine.Rows.Count > 0)
    //                {
    //                    for (int k = 0; k < dataLine.Rows.Count; k++)
    //                    {
    //                        sb.Append("{");

    //                        for (int l = 0; l < dataApp.Columns.Count; l++)
    //                        {
    //                            sb.Append("\"" + dataApp.Columns[l].ColumnName.ToString() + "\":\"" + dataApp.Rows[k][l].ToString() + "\",");

    //                        }
    //                    }
    //                    sb.Remove(sb.Length - 1, 1);
    //                }
    //                sb.Append("]");
    //                sb.Append("},");
    //            }
    //            sb.Remove(sb.Length - 1, 1);

    //        }
    //        sb.Append("],");
    //        Message = "获取数据成功";
    //    }
    //    catch (Exception ex)
    //    {
    //        Message = "获取数据失败:" + ex.Message;
    //    }
    //    sb.AppendFormat(" \"Message\":\"{0}\"", Message);
    //    //sb.Append(",");
    //    ////sb.AppendFormat(" \"DisabledVisitIds\":\"{0}\"", DisabledVisitsId);
    //    ////sb.Append(",");
    //    //sb.AppendFormat(" \"LastUpdateTime\":\"{0}\"", returnTimestamp);
    //    sb.Append("}");
    //    return sb.ToString();
    //}



    [WebMethod]
    [System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetHistoryOrderListPage(int userId, long orderDate, int pageSize, int currentPage)
    {

        StringBuilder sb = new StringBuilder();
        //返回消息内容
        String Message = "";
        sb.Append("{");
        try
        {
            DataTable dataApp = new OrderHeaderManager().GetHistoryOrderList(userId, orderDate, pageSize, currentPage);
            sb.Append(" \"HistoryOrderList\":[");
            if (dataApp != null && dataApp.Rows.Count > 0)
            {
                for (int i = 0; i < dataApp.Rows.Count; i++)
                {
                    sb.Append("{");
                    String orderNo = "";
                    for (int j = 0; j < dataApp.Columns.Count; j++)
                    {
                        sb.Append("\"" + dataApp.Columns[j].ColumnName.ToString() + "\":\"" + dataApp.Rows[i][j].ToString() + "\",");
                        if ("ORDER_NO" == dataApp.Columns[j].ColumnName)
                        {
                            orderNo = dataApp.Rows[i][j].ToString();
                        }
                    }
                    DataTable dataLine = new OrderLineManager().GetOrderLineList(orderNo);
                    sb.Append(" \"OrderLineList\":[");
                    if (dataLine != null && dataLine.Rows.Count > 0)
                    {
                        for (int k = 0; k < dataLine.Rows.Count; k++)
                        {
                            sb.Append("{");

                            for (int l = 0; l < dataLine.Columns.Count; l++)
                            {
                                sb.Append("\"" + dataLine.Columns[l].ColumnName.ToString() + "\":\"" + dataLine.Rows[k][l].ToString() + "\",");

                            }
                            sb.Remove(sb.Length - 1, 1);
                            sb.Append("},");
                        }

                        sb.Remove(sb.Length - 1, 1);
                    }

                    sb.Append("]");
                    sb.Append("},");
                }
                sb.Remove(sb.Length - 1, 1);

            }
            sb.Append("],");
            Message = "获取数据成功";
        }
        catch (Exception ex)
        {
            Message = "获取数据失败:" + ex.Message;
        }
        sb.AppendFormat(" \"Message\":\"{0}\"", Message);
        //sb.Append(",");
        ////sb.AppendFormat(" \"DisabledVisitIds\":\"{0}\"", DisabledVisitsId);
        ////sb.Append(",");
        //sb.AppendFormat(" \"LastUpdateTime\":\"{0}\"", returnTimestamp);
        sb.Append("}");
        return sb.ToString();
    }
}
